#define  _CRT_SECURE_NO_WARNINGS
#include "linklist_mergesort.h"
#include "array_sort.h"
#include "string.h"
#include <iostream>
#include <string>
#include <cstring>

void test_bsearch();
void test_quicksort_2();

void test0() { //merge sort on a linked list
	int n;
	do {
		std::cout << "Enter the #elements of the list[negative number to quit]: ";
		std::cin >> n;
		Node* head = build_random_list(n);
		print_list(head);
		mergesort(head,head->value);
		print_list(head);
	} while (n>=0);
}

void sort_print(int unsorted[], int n, void (*func)(int a[], int left, int right) ){
	int* a = (int*)malloc(n*sizeof(int));
	for(int i = 0; i < n; i++)
		a[i] = unsorted[i];
	func(a,0,n-1);
	for(int i = 0; i < n; i++) 
		std::cout << a[i] << ' ';
	std::cout << std::endl;
	free(a);
}
void test1() { //sorting
	int n;
	do {
		std::cout << "Enter the #elements of the array[nonpositive number to quit]: ";
		std::cin >> n;
		if(n<=0) break;
		int* a = (int*)malloc(n*sizeof(int));
		build_random_array(a,n,100);
		for(int i = 0; i < n; i++) 
			std::cout << a[i] << ' ';
		std::cout << std::endl;
		sort_print(a,n,quicksort);
		sort_print(a,n,quicksort_right_pivot);
		sort_print(a,n,mergesort);
		free(a);

	}while(true);
}

void test2(){ //binary search
	int a[10];
	for(int i = 0; i < 10; i++)
		a[i] = 2*i+1;
	while(true) {
		int key;
		std::cout << "enter the key to search:" << std::endl;
		std::cin >> key;
		if(key<0) break;
		std::cout << binary_search(a,10,key) << std::endl;
	}
}

void test3(){ //string to integer
	std::string str;
	while(true){
		std::cin >> str;
		char* cstr = new char[str.length()+1];
		strcpy(cstr, str.c_str());
		std::cout << string2int(cstr) ;
		std::cout << " err status= " << err_status << std::endl;
	}
}

int main(){
	/*test0();*/
	//test1();
	/*test2();*/
	/*test3();*/
	//test_counting_sort();
	//test_bsearch();
	test_quicksort_2();
	return 0;
}